package com.myblog.demo.service;

import com.myblog.demo.common.AjaxResult;
import com.myblog.demo.entity.vo.Following;
import com.myblog.demo.entity.vo.Follows;
import com.myblog.demo.mapper.FollowMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class FollowService {

    @Autowired
    private FollowMapper followMapper;

    // userId 关注 followingId
    public int addFollowRelationship(Integer userId, Integer followingId) {
        // 判断 userId 是否已经关注了 followingId
        if (followMapper.findFollowRelationship(userId, followingId) != null) {
            return 0;
        }
        return followMapper.addFollowRelationship(userId, followingId);
    }

    // userId 取消关注 followingId
    public int delFollowRelationship(Integer userId, Integer followingId) {
        return followMapper.delFollowRelationship(userId, followingId);
    }

    // 获取 userId 关注的作者列表
    public List<Following> getFollowingList(Integer userId) {
        return followMapper.getFollowingList(userId);
    }

    // 获取 userId 的粉丝列表
    public List<Follows> getFollowsList(Integer userId) {
        List<Follows> list = followMapper.getFollowsList(userId);

        for (Follows follows : list) {
            int followsId = follows.getFollowsId();
            Integer flag = followMapper.findFollowRelationship(userId, followsId);
            System.out.println(flag);
            // 如果查询结果为空，说明未关注其粉丝，设置为false
            follows.setIsFollow(flag != null);
        }
        return list;
    }

    // 判断当前用户是否关注 followingId 用户
    public Boolean isFollowing(Integer userId, Integer followingId) {
        Integer flag = followMapper.findFollowRelationship(userId, followingId);
        return flag != null;
    }

    // 判断两人是否相互关注
    public Boolean isFollowEachOther(Integer userId1, Integer userId2) {
        Integer flag1 = followMapper.findFollowRelationship(userId1, userId2);
        if (flag1 == null){
            return false;
        }
        Integer flag2 = followMapper.findFollowRelationship(userId2, userId1);
        if (flag2 == null){
            return false;
        }

        return true;
    }
}